package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.graphics.Paint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityBreedingRecordBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterBreedingRecord
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/9/9 10:11
 * @Describe    : 作育记录
 */
class ActivityBreedingRecord : BaseActivity<BreedingPigeonViewModel, ActivityBreedingRecordBinding>() {

    private var femaleName: String = ""
    private var maleName: String = ""
    private var imageUrl: String = ""
    private var breedingPairId = 0
    private val mAdapter: AdapterBreedingRecord by lazy { AdapterBreedingRecord() }
    private lateinit var loadSir: LoadService<Any>
    private var mMalePigeonId = 0
    private var mFemalePigeonId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "作育记录"

        mDatabind.tvFemaleRing.paint.flags = Paint.UNDERLINE_TEXT_FLAG//设置下划线
        mDatabind.tvMaleRing.paint.flags = Paint.UNDERLINE_TEXT_FLAG//设置下划线
        breedingPairId = intent.getIntExtra("breedingId", 0)
        mViewModel.getBreedingPairSub(breedingPairId)
        loadSir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            mViewModel.getBreedingPairSub(breedingPairId)
            onRefresh(true)
        }

        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 12f)))
            it.initFooter {
                onRefresh(false)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            mViewModel.getBreedingPairSub(breedingPairId)
            onRefresh(true)
        }

        mAdapter.addChildClickViewIds(R.id.ivDeleteInfoItem)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.ivDeleteInfoItem -> showDeleteDialog(mAdapter.data[position].breedingPairSubId.toString())
            }
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityBreedingTracking>(
                "pairId" to breedingPairId,
                "maleName" to maleName,
                "maleRingId" to mDatabind.tvMaleRing.text.toString().trim(),
                "femaleName" to femaleName,
                "femaleRingId" to mDatabind.tvFemaleRing.text.toString().trim(),
                "breedingPairSubId" to mAdapter.data[position].breedingPairSubId.toString(),
                "femaleId" to mFemalePigeonId,
                "maleId" to mMalePigeonId
            )
        }

        eventViewModel.toRefreshData.observeInActivity(this) {
            onRefresh(true)
            mViewModel.getBreedingPairSub(breedingPairId)
        }
    }

    private fun showDeleteDialog(id: String) {
        val dialog = RxDialogDefault(this)
        dialog.setTitle("确认删除该条作育记录信息吗?")
        dialog.setSureListener {
            dialog.dismiss()
            mViewModel.delBreedingPairSubInfo(id)
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh(b: Boolean) {
        if (b)
            loadSir.showLoading()
        mViewModel.getBreedingPairSubList(b, breedingPairId)
    }

    override fun createObserver() {
        super.createObserver()
        /** 删除作育记录信息 */
        mViewModel.delBreedingPairSubInfoResult.observe(this) {
            onRefresh(true)
        }
        /** 获取作育记录列表 */
        mViewModel.getBreedingPairSubDataResult.observe(this) {
            mDatabind.data = it
            imageUrl = it.maleUrl
            mMalePigeonId = it.maleId
            mFemalePigeonId = it.femaleId
            ImageLoaderManager.loadRoundImage(this, imageUrl, mDatabind.ivSubImage, 6)
            maleName = it.maleName.trim()
            femaleName = it.femaleName.trim()
            mDatabind.tvSubNamePair.text = if (maleName.isEmpty()) "暂无昵称" else maleName + "和" + femaleName.ifEmpty { "暂无昵称" } + "配对"
        }
        /** 获取作育记录列表 */
        mViewModel.getBreedingPairSubResult.observe(this) {
            loadListData(it, mAdapter, loadSir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.tvFemaleRing, mDatabind.tvMaleRing, mDatabind.ivSubImage, mDatabind.btnStartRear) {
            when (it) {
                mDatabind.btnStartRear -> startActivity<ActivityBreedingTracking>(
                    "pairId" to breedingPairId,
                    "maleName" to maleName,
                    "maleRingId" to mDatabind.tvMaleRing.text.toString().trim(),
                    "femaleName" to femaleName,
                    "femaleRingId" to mDatabind.tvFemaleRing.text.toString().trim(),
                    "femaleId" to mFemalePigeonId,
                    "maleId" to mMalePigeonId
                )

                mDatabind.ivSubImage -> openBigImage(mDatabind.ivSubImage, imageUrl)
                mDatabind.tvMaleRing -> startActivity<ActivityBloodPigeonInfo>("pigeonId" to mMalePigeonId, "isAmend" to false)
                mDatabind.tvFemaleRing -> startActivity<ActivityBloodPigeonInfo>("pigeonId" to mFemalePigeonId, "isAmend" to false)
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}